package edu.tongji.engine.def.parser;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import com.wutka.jox.JOXBeanInputStream;

import edu.tongji.engine.common.Log;
import edu.tongji.engine.db.dao.DAOFactory;
import edu.tongji.engine.db.dao.IActorDao;
import edu.tongji.engine.db.pojo.Actor;
import edu.tongji.engine.def.ActivityNode;
import edu.tongji.engine.def.EndNode;
import edu.tongji.engine.def.ForkNode;
import edu.tongji.engine.def.JoinNode;
import edu.tongji.engine.def.Node;
import edu.tongji.engine.def.ProcessDefinition;
import edu.tongji.engine.def.StartNode;
import edu.tongji.engine.def.Transition;


public class ProcessParser {

	public ProcessDefinition parser(String fileName){
		try{
		FileInputStream in = new FileInputStream(fileName);
		JOXBeanInputStream joxIn = new JOXBeanInputStream(in);
		ProcessXML process = (ProcessXML)joxIn.readObject(ProcessXML.class);
		return this.mappingProcess(process);
		}
		catch(Exception e){
			System.out.println(e.toString());
			Log.getLoger().loger.error("EnterEvent Error", e);
			return null;
		}	
	}
	
	public  ProcessDefinition mappingProcess(ProcessXML processXML){
		ProcessDefinition pd = new ProcessDefinition();
		pd.setName(processXML.getName());
		pd.setDescription(processXML.getDescription());
		
		Map<String,Transition> map = new HashMap(0);
		for( TransitionXML transXML:processXML.getTransList() ){
			Transition trans = new Transition();
			trans.setName(transXML.getName());
			trans.setDescription(transXML.getDescription());
			trans.setPd(pd);
			map.put(transXML.getName(), trans);
		}
		
		for( NodeXML nodeXML:processXML.getNodeList() ){
			Node node = null;
			
			if (nodeXML.getType().equals("StartNode")){
				node = new StartNode();
				node.setName(nodeXML.getName());
				node.setDescription(nodeXML.getDescription());
				node.setEnterEventHandler(nodeXML.getEnterClass());
				node.setLeaveEventHandler(nodeXML.getLeaveClass());
				node.setPd(pd);
				pd.setStartNode(node);
				System.out.println(pd.getName());
				node.setArrivingTransition(null);
				node.setleavingTransition(map.get(nodeXML.getTo_trans().get(0)));
				
				
			}else if (nodeXML.getType().equals("ActivityNode")){
				node = new ActivityNode();
				node.setName(nodeXML.getName());
				node.setDescription(nodeXML.getDescription());
				node.setEnterEventHandler(nodeXML.getEnterClass());
				node.setLeaveEventHandler(nodeXML.getLeaveClass());
				node.setPd(pd);
				Actor actor = null;
				if (nodeXML.getActorId()!=null)
					actor = this.getActorInfo(nodeXML.getActorId());
				((ActivityNode)node).setActor(actor);
				((ActivityNode)node).setMail(nodeXML.getMail());
				node.setArrivingTransition(map.get(nodeXML.getFrom_trans().get(0)));
				node.setleavingTransition(map.get(nodeXML.getTo_trans().get(0)));
				
			}else if(nodeXML.getType().equals("ForkNode")){
				node = new ForkNode();
				node.setName(nodeXML.getName());
				node.setDescription(nodeXML.getDescription());
				node.setEnterEventHandler(nodeXML.getEnterClass());
				node.setLeaveEventHandler(nodeXML.getLeaveClass());
				node.setPd(pd);
				node.setArrivingTransition(map.get(nodeXML.getFrom_trans().get(0)));
				for(String trans:nodeXML.getTo_trans()){
					node.addLeavingTransition(map.get(trans));
				}
				
			}else if(nodeXML.getType().equals("JoinNode")){
				node = new JoinNode();
				node.setName(nodeXML.getName());
				node.setDescription(nodeXML.getDescription());
				node.setEnterEventHandler(nodeXML.getEnterClass());
				node.setLeaveEventHandler(nodeXML.getLeaveClass());
				node.setPd(pd);
				node.setleavingTransition(map.get(nodeXML.getTo_trans().get(0)));
				for(String trans:nodeXML.getFrom_trans()){
					node.addArrivingTransition(map.get(trans));
				}
				
			}else if(nodeXML.getType().equals("EndNode")){
				node = new EndNode();
				node.setName(nodeXML.getName());
				node.setDescription(nodeXML.getDescription());
				node.setEnterEventHandler(nodeXML.getEnterClass());
				node.setLeaveEventHandler(nodeXML.getLeaveClass());	
				node.setPd(pd);
				node.setArrivingTransition(map.get(nodeXML.getFrom_trans().get(0)));
				node.setleavingTransition(null);
				
			}
			pd.addNode(node);
		}
		return pd;
	}
	
	public Actor getActorInfo(Long id){
		IActorDao dao= DAOFactory.createActorDao();
		Actor actor = dao.getActor(id);
		if (actor == null){
			System.out.println("actorId is invalid!!");
		}
		return actor;
	}
}
